# -*-coding:utf-8-*-
import collections

s = ['a', 'e', 'a', 'd', 'c', 'r', 'w', 'h', 'h', 'a', 'c', 'n', 'f', 'd', 'e', 't', 'a', 'c']
# 方法一，利用字典
dic1 = dict()
for c in s:
    if c in dic1:
        dic1[c] += 1
    else:
        dic1[c] = 1
print(dic1)

# 方法二，利用字典+count()
dic2 = dict()
for c in s:
    if c in dic2:
        continue
    dic2[c] = s.count(c)
print(dic2)

# 方法三，利用collections模块的Counter类,Counter类继承自dict
dic3 = collections.Counter(s)
print(dic3)

# 按次数正序排序
list_sort = sorted(dic3.items(), key=lambda x: x[1], reverse=False)
print("排序后：{}".format(list_sort))
